package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.RoomBean;
import com.project.mapper.IClassMapper;
import com.project.mapper.IRoomMapper;
import com.project.mapper.IStudentMapper;
import com.project.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class RoomServiceImp implements IRoomService{
    @Autowired
    private IRoomMapper roomMapper;
    @Autowired
    private IStudentMapper studentMapper;
    @Autowired
    private IClassMapper classMapper;


    @Override
    public IPage<RoomBean> cutFind(Integer pageNum, String roomAddress, String roomStatus, String roomGender, String roomCanIn) {

        return roomMapper.cutFind(new Page(pageNum,2) ,roomAddress, roomStatus, roomGender, roomCanIn);



    }

    @Override
    public void add(RoomBean roomBean) {
        roomMapper.insert(roomBean);
    }

    @Override
    public boolean del(Integer roomID) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("fk_roomid",roomID);
        List list = studentMapper.selectList(qw);
        if (list.size() == 0) {
            roomMapper.deleteById(roomID);
            return true;
        }
        return false;
    }

    @Override
    public RoomBean findByID(Integer roomID) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("fk_roomid",roomID);
        List list = studentMapper.selectList(qw);
//        List list = studentMapper.findByRoomID();
        RoomBean roomBean = roomMapper.selectById(roomID);
        roomBean.setRoomStudents(list);
        roomBean.setRoomInNum(list.size());
        return roomBean;
    }

    @Override
    public List<RoomBean> findByGender(String roomGender) {
        return roomMapper.findByGender(roomGender);
    }
}
